package MyCustomTable;

import MyUtil.*;
import redis.clients.jedis.Jedis;

import javax.swing.*;
import java.awt.*;
import java.sql.*;

public class Manager_password_Set extends JFrame{
    //sql语句
    private String sql ="select Eaccount 员工账号,Ename 员工姓名,Epassword from Employee where Elevel ='1' or Eaccount = ?";
    private String sql2 = "Update Employee set Epassword = ? where Eaccount = ? and (Elevel !='1' or Eaccount = ?)";
    private String sql3 = "delete from Employee where Eaccount = ?";

    private String eaccount = null;
    private JScrollPane jScrollPane;

    private JButton change_button = new JButton("修改(根据)");
    private JLabel tip_label = new JLabel("提示:不可修改管理员密码(自己除外,且密码30字符以内),删除员工Eid也是用修改的那个文本域");
    private JLabel eid_label = new JLabel("想要修改的员工账号：");
    private JLabel epassword_label = new JLabel("修改成的密码:");

    private JTextField eid_TextField = new JTextField(10);
    private JTextField epassword_TextField = new JTextField(10);
    //删除按钮
    private JButton delete_button = new JButton("删除");

    private JPanel myPanel;
    private JPanel myPanel2;
    public Manager_password_Set(String Eaccount) throws SQLException {
        super("员工修改及删除");

        eaccount =Eaccount;
        //基础设置
        this.setBounds(400,200,1100,600);
        //设置控件
        myPanel = new MyPanel();
        this.setContentPane(myPanel);
        myPanel2 = new MyPanel("/picture/5.jpg");
        //设置布局
        myPanel.setLayout(new BorderLayout());
        myPanel2.setLayout(new FlowLayout());
        //ScrollPane的设置
        jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.prepareResultSet(sql,eaccount));
        //加入控件
        myPanel.add(tip_label,BorderLayout.PAGE_START);
        myPanel.add(jScrollPane,BorderLayout.WEST);
        myPanel.add(myPanel2,BorderLayout.EAST);

        myPanel2.add(eid_label);
        myPanel2.add(eid_TextField);
        myPanel2.add(epassword_label);
        myPanel2.add(epassword_TextField);
        myPanel2.add(change_button);
        myPanel2.add(delete_button);
        //button设置
        change_button.addActionListener((e)->{
            String p1 = eid_TextField.getText();
            String p2 = epassword_TextField.getText();
            String[] p3 =  new String[3];
            p3[0] = p2;
            p3[1] = p1;
            p3[2] = eaccount;
            if((p1.equals("")) ||(p2.equals(""))){
                JOptionPane.showMessageDialog(this,"有输入值为空","错误",JOptionPane.PLAIN_MESSAGE);
            }
            else{
                try {
                    Jedis jedis = RedisUtil.getConRedis();
                    jedis.lset(p1,0,p2);
                    DataBaseUtil.changeResultSet(sql2,p3);
                    myPanel.remove(jScrollPane);
                    jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.prepareResultSet(sql,eaccount));
                    myPanel.add(jScrollPane,BorderLayout.WEST);
                    this.setVisible(false);
                    this.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        delete_button.addActionListener((e)->{
            String p1 = eid_TextField.getText();
            if((p1.equals(""))){
                JOptionPane.showMessageDialog(this,"输入值为空","错误",JOptionPane.PLAIN_MESSAGE);
            }
            else{
                try {
                    Jedis jedis = RedisUtil.getConRedis();
                    jedis.del(p1);
                    DataBaseUtil.changeResultSet(sql3,p1);
                    myPanel.remove(jScrollPane);
                    jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.prepareResultSet(sql,eaccount));
                    myPanel.add(jScrollPane,BorderLayout.WEST);
                    this.setVisible(false);
                    this.setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        this.setVisible(true);

    }
}
